﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Automation.BDaq;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SerialPortCommunicator serialPort = null;
        SignalMonitor signalMonitor = null;
        public Form1()
        {
            InitializeComponent();
        }
     
        private void Form1_Load(object sender, EventArgs e)
        {
            #region 启动串口
            serialPort = new SerialPortCommunicator();
            serialPort.Start();
            #endregion

            #region 持续接受信号
            signalMonitor = new SignalMonitor(this);
            signalMonitor.StartMonitoring();
            signalMonitor.output_updown(4, 1);
            #endregion
        }
        
        #region com模块
        private void button_start_Click(object sender, EventArgs e)
        {
            //开始{ 0x01, 0x06, 0x20, 0x00, 0x00, 0x12 };
            //停止{ 0x01, 0x06, 0x20, 0x00, 0x00, 0x11 };
            //频率修改{ 0x01, 0x06, 0x20, 0x01, 0x00, 0x11 };
            byte[] dataToSend1 = new byte[6] { 0x01, 0x06, 0x20, 0x00, 0x00, 0x12 };//开始
            dataToSend1[0] = (byte)comboBox_站号.SelectedIndex;
            serialPort.SendData(dataToSend1);
        }
        private void button_stop_Click(object sender, EventArgs e)
        {
            //开始{ 0x01, 0x06, 0x20, 0x00, 0x00, 0x12 };
            //停止{ 0x01, 0x06, 0x20, 0x00, 0x00, 0x11 };
            //频率修改{ 0x01, 0x06, 0x20, 0x01, 0x00, 0x11 };
            byte[] dataToSend1 = new byte[6] { 0x01, 0x06, 0x20, 0x00, 0x00, 0x11 };//停止
            dataToSend1[0] = (byte)comboBox_站号.SelectedIndex;
            serialPort.SendData(dataToSend1);
        }
        private void button_change_Click(object sender, EventArgs e)
        {

            //开始{ 0x01, 0x06, 0x20, 0x00, 0x00, 0x12 };
            //停止{ 0x01, 0x06, 0x20, 0x00, 0x00, 0x11 };
            //频率修改{ 0x01, 0x06, 0x20, 0x01, 0x00, 0x11 };
            byte[] dataToSend1 = new byte[6] { 0x01, 0x06, 0x20, 0x01, 0x00, 0x11 };//频率修改
            dataToSend1[0] = (byte)comboBox_站号.SelectedIndex;

            int intValue = 100 * Convert.ToInt32(textBox_fre.Text);
            // 将 int 转换成两个字节 (高位在前)
            byte highByte = (byte)((intValue >> 8) & 0xFF); // 获取高位字节
            byte lowByte = (byte)(intValue & 0xFF);         // 获取低位字节

            // 将高位和低位分别放入 dataToSend 数组的最后两个位置
            dataToSend1[4] = highByte;
            dataToSend1[5] = lowByte;
            serialPort.SendData(dataToSend1);
        }
        public void port_send(int num, int select, int value = 0)
        {
            switch (select)
            {
                case 1://start
                    {
                        byte[] dataToSend1 = new byte[6] { 0x01, 0x06, 0x20, 0x00, 0x00, 0x12 };//开始
                        dataToSend1[0] = (byte)num;
                        serialPort.SendData(dataToSend1);
                    }
                    break;
                case 2://stop
                    {
                        byte[] dataToSend1 = new byte[6] { 0x01, 0x06, 0x20, 0x00, 0x00, 0x11 };//停止
                        dataToSend1[0] = (byte)num;
                        serialPort.SendData(dataToSend1);
                    }
                    break;
                case 3://change
                    {
                        byte[] dataToSend1 = new byte[6] { 0x01, 0x06, 0x20, 0x01, 0x00, 0x11 };//频率修改
                        dataToSend1[0] = (byte)num;
                        // 将 int 转换成两个字节 (高位在前)
                        byte highByte = (byte)((value >> 8) & 0xFF); // 获取高位字节
                        byte lowByte = (byte)(value & 0xFF);         // 获取低位字节

                        // 将高位和低位分别放入 dataToSend 数组的最后两个位置
                        dataToSend1[4] = highByte;
                        dataToSend1[5] = lowByte;
                        serialPort.SendData(dataToSend1);
                    }
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 手动控制的启动信号
        #region 上料1
        private void button上料1顶升_Click(object sender, EventArgs e)
        {
            signalMonitor.output_updown(0, 1);
        }

        private void button上料1回落_Click(object sender, EventArgs e)
        {
            signalMonitor.output_updown(0, 0);
        }

        private void button上料1上移_Click(object sender, EventArgs e)
        {
            port_send(1, 1);
        }

        private void button上料1停止上移_Click(object sender, EventArgs e)
        {
            port_send(1, 2);
        }

        private void button上料1右移_Click(object sender, EventArgs e)
        {
            port_send(2, 1);
        }

        private void button上料1右移停止_Click(object sender, EventArgs e)
        {
            port_send(2, 2);
        }
        #endregion

        #region 上料2
        private void button上料2顶升_Click(object sender, EventArgs e)
        {
            signalMonitor.output_updown(1, 1);
        }
        private void button上料2回落_Click(object sender, EventArgs e)
        {
            signalMonitor.output_updown(1, 0);
        }
        private void button上料2上移_Click(object sender, EventArgs e)
        {
            port_send(3, 1);
        }
        private void button上料2停止上移_Click(object sender, EventArgs e)
        {
            port_send(3, 2);
        }
        private void button上料2右移_Click(object sender, EventArgs e)
        {
            port_send(4, 1);
        }
        private void button上料2右移停止_Click(object sender, EventArgs e)
        {
            port_send(4, 2);
        }
        #endregion

        #region 上料3
        private void button上料3顶升_Click(object sender, EventArgs e)
        {
            signalMonitor.output_updown(2, 1);
        }

        private void button上料3回落_Click(object sender, EventArgs e)
        {
            signalMonitor.output_updown(2, 0);
        }

        private void button上料3上移_Click(object sender, EventArgs e)
        {
            port_send(5, 1);
        }

        private void button上料3停止上移_Click(object sender, EventArgs e)
        {
            port_send(5, 2);
        }

        private void button上料3右移_Click(object sender, EventArgs e)
        {
            port_send(6, 1);
        }

        private void button上料3右移停止_Click(object sender, EventArgs e)
        {
            port_send(6, 2);
        }

        #endregion

        #region 缓存1
        private void button缓存1启动_Click(object sender, EventArgs e)
        {
            port_send(7, 1);
        }

        private void button缓存1停止_Click(object sender, EventArgs e)
        {
            port_send(7, 2);
        }

        #endregion

        #region 缓存2
        private void button缓存2启动_Click(object sender, EventArgs e)
        {
            port_send(8, 1);
        }

        private void button缓存2停止_Click(object sender, EventArgs e)
        {
            port_send(8, 2);
        }

        #endregion

        #region 缓存3
        private void button缓存3上移_Click(object sender, EventArgs e)
        {
            port_send(14, 1);
        }

        private void button缓存3上移停止_Click(object sender, EventArgs e)
        {
            port_send(14, 2);
        }

        private void button缓存3右移_Click(object sender, EventArgs e)
        {
            port_send(13, 1);
        }
        private void button缓存3右移停止_Click(object sender, EventArgs e)
        {
            port_send(13, 2);
        }

        private void button缓存3顶升_Click(object sender, EventArgs e)
        {
            signalMonitor.output_updown(3, 1);
        }

        private void button缓存3回落_Click(object sender, EventArgs e)
        {
            signalMonitor.output_updown(3, 0);
        }

        #endregion

        #region 缓存4
        private void button缓存4启动_Click(object sender, EventArgs e)
        {
            port_send(15, 1);
        }

        private void button缓存4停止_Click(object sender, EventArgs e)
        {
            port_send(15, 2);
        }

        #endregion

        #region 中转1
        private void button中转1启动_Click(object sender, EventArgs e)
        {
            port_send(12, 1);
        }

        private void button中转1停止_Click(object sender, EventArgs e)
        {
            port_send(12, 2);
        }

        #endregion

        #region 中转2
        private void button中转2启动_Click(object sender, EventArgs e)
        {
            port_send(11, 1);
        }

        private void button中转2停止_Click(object sender, EventArgs e)
        {
            port_send(11, 2);
        }
        #endregion

        #region 中转3
        private void button中转3顶升_Click(object sender, EventArgs e)
        {
            signalMonitor.output_updown(4, 1);
        }

        private void button中转3回落_Click(object sender, EventArgs e)
        {
            signalMonitor.output_updown(4, 0);
        }

        private void button中转3上移_Click(object sender, EventArgs e)
        {
            port_send(10, 1);
        }

        private void button中转3上移停止_Click(object sender, EventArgs e)
        {
            port_send(10, 2);
        }

        private void button中转3右移_Click(object sender, EventArgs e)
        {
            port_send(9, 1);
        }

        private void button中转3右移停止_Click(object sender, EventArgs e)
        {
            port_send(9, 2);
        }
        #endregion

        #region 委托
        public void all_enabled()
        {
            Task.Factory.StartNew(() =>
            {
                this.Invoke(new Action(() =>
                {
                    this.Enabled = false;
                }));
            });
        }
        public void all_abled()
        {
            Task.Factory.StartNew(() =>
            {
                this.Invoke(new Action(() =>
                {
                    this.Enabled = true;
                }));
            });
        }
        #endregion

        #endregion

        public void button3_Red()
        {
            this.Invoke(new Action(() =>
            {
                button急停.BackColor = Color.Red;
            }));
        }
        public void button3_Green()
        {
            this.Invoke(new Action(() =>
            {
                button急停.BackColor = Color.LightGreen;
            }));
        }

    }
}